package com.example.lyl.yuejian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.BuildConfig;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.MasterLvBean;
import com.example.lyl.yuejian.view.home.MasterDetialActivity;

/**
 * Created by dllo on 18/3/23.
 */

public class MasterRvAdapter extends RecyclerView.Adapter<MasterRvAdapter.Holder> {

    MasterLvBean masterLvBean;
    Context context;
    private View itemView;

    public void setMasterLvBean(MasterLvBean masterLvBean) {
        this.masterLvBean = masterLvBean;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_matchlv,parent,false);
        Holder holder = new Holder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        Glide.with(context).load("http://" + masterLvBean.getData().get(position)).placeholder(R.mipmap.lunbo1).into(holder.matchIv);
        if (BuildConfig.DEBUG)
            Log.d("MatchLvAdapter", masterLvBean.getData().get(position).getMname());
        holder.matchTv.setText(masterLvBean.getData().get(position).getMname());
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("mid",masterLvBean.getData().get(position).getMid());
                intent.setClass(context, MasterDetialActivity.class);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (masterLvBean == null){
            return 0;
        }else {
            return masterLvBean.getData().size();
        }
    }

    class Holder extends RecyclerView.ViewHolder{
        TextView matchTv;
        ImageView matchIv;
        public Holder(View itemView) {
            super(itemView);
            matchIv = (ImageView) itemView.findViewById(R.id.match_iv);
            matchTv = (TextView) itemView.findViewById(R.id.match_tv);
        }
    }
}
